package regpang.listviewtest;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.coszero.utilslibrary.base.BaseRecyclerAdapter2;
import com.coszero.utilslibrary.base.BaseRecyclerViewHolder;
import com.coszero.utilslibrary.callback.OnItemClickListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import regpang.listviewtest.DragRecycler.DragRecyclerActivity;
import regpang.listviewtest.GrideLayout.GrideActivity;
import regpang.listviewtest.LinearLayout.LinearListActivity;
import regpang.listviewtest.StaggeredLayout.StaggeredActivity;
import regpang.listviewtest.SwipeRecyclerView.SwipeRecyclerActivity;
import regpang.listviewtest.decoratio.DividerItemDecoration;

public class RecyclerMainActivity extends AppCompatActivity {

    private List<String> items;
    private List<Class> activities = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_recycler);
        setTitle("Recycler功能样例");
        addActivitys();
        String[] stringArray = getResources().getStringArray(R.array.recyclerList);
        items = Arrays.asList(stringArray);
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                //这里可以控制是否允许滑动
                return true;
            }
        });
        //分割线
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        BaseRecyclerAdapter2 baseRecyclerAdapter = new BaseRecyclerAdapter2<String>(this, items) {
            @Override
            public int getItemLayoutId(int viewType) {
                return android.R.layout.simple_list_item_1;
            }

            @Override
            public void bindData(BaseRecyclerViewHolder holder, int position, String item) {
                holder.getTextView(android.R.id.text1).setText(item);
            }
        };
        recyclerView.setAdapter(baseRecyclerAdapter);
        baseRecyclerAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position,BaseRecyclerViewHolder rv) {
                System.out.print("MainList  position:" + position + " position:" + position);
                startActivity(new Intent(RecyclerMainActivity.this, activities.get(position)));
            }
        });
    }

    /**
     * 添加待跳转的Activity
     */
    private void addActivitys() {
        activities.add(LinearListActivity.class);
        activities.add(GrideActivity.class);
        activities.add(StaggeredActivity.class);
        activities.add(DragRecyclerActivity.class);
        activities.add(SwipeRecyclerActivity.class);
    }
}
